package com.example.rbacmanage.service.app.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.rbacmanage.annotation.PermissionRange;
import com.example.rbacmanage.entity.app.MinAppConfig;
import com.example.rbacmanage.entity.app.ShopQrCode;
import com.example.rbacmanage.mapper.app.MinAppConfigMapper;
import com.example.rbacmanage.mapper.app.ShopQrCodeMapper;
import com.example.rbacmanage.property.DefinedProperty;
import com.example.rbacmanage.service.app.ShopQrCodeService;
import com.example.rbacmanage.util.AlipayUtil;
import com.example.rbacmanage.util.FileUtils;
import com.example.rbacmanage.util.WxUtil;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ShopQrCodeServiceImpl implements ShopQrCodeService {
    @Autowired
    private ShopQrCodeMapper shopQrCodeMapper;
    @Autowired
    private MinAppConfigMapper minAppConfigMapper;
    @Autowired
    private DefinedProperty definedProperty;

    //应用id
    @Value("${minapp.appId}")
    private String appId;
    //应用秘钥
    @Value("${minapp.appSecret}")
    private String appSecret;
    @SneakyThrows
    @Override
    public void save(ShopQrCode shopQrCode) {
        shopQrCode.setCreateTime(new Date());
        //桌号不能重复添加
        ShopQrCode queryShopQrCode = new ShopQrCode();
        queryShopQrCode.setShopId(shopQrCode.getShopId());
        queryShopQrCode.setTableNumber(shopQrCode.getTableNumber());
        queryShopQrCode.setQrCodeType(shopQrCode.getQrCodeType());
        Integer count = shopQrCodeMapper.count(queryShopQrCode);
        if (count != null && count > 0) {
            throw new Exception("该店铺-该小程序类型-该桌号已添加，无须再加");
        }
        if(shopQrCode.getShopId() != null){
            //暂时注释
            MinAppConfig minAppConfig = minAppConfigMapper.selectByShopIdAndType(shopQrCode.getShopId(),shopQrCode.getQrCodeType());
            if(minAppConfig == null){
                throw new Exception("请先配置该店铺的相关小程序配置，再添加");
            }
            String content = "shopId="+shopQrCode.getShopId()+"&tableNumber="+shopQrCode.getTableNumber();
            if (queryShopQrCode.getQrCodeType().equals("0")){
                String base64 = WxUtil.getQrcodeUrl(appId,appSecret,content);
                if (!StringUtils.isEmpty(base64)) {
                    String dir = FileUtils.createBaseDateFileMkdir(definedProperty.getUploadProfile());
                    String filePath = dir.concat("/") +System.currentTimeMillis()+".png";
                    FileUtils.base64ToFile(base64,filePath);
                    String base64ToFilePath = filePath.replace(definedProperty.getUploadProfile(), "/profile/");
                    shopQrCode.setQrCode(base64ToFilePath);
                }
            }else if (queryShopQrCode.getQrCodeType().equals("1")){
                String qrCode = AlipayUtil.createQrCode(minAppConfig.getAppId(),minAppConfig.getPrivateKeyPath(),minAppConfig.getPrivateKeyPath(),content);
            }
        }
        shopQrCodeMapper.insert(shopQrCode);
    }

    @Override
    public void remove(Long[] ids) {
        for (Long id : ids) {
            shopQrCodeMapper.del(id);
        }
    }

    @Override
    public void update(ShopQrCode shopQrCode) {
        shopQrCodeMapper.update(shopQrCode);
    }

    @Override
    public List<ShopQrCode> getAll(ShopQrCode shopQrCode) {
        return shopQrCodeMapper.list(shopQrCode);
    }

    @PermissionRange(shopAlias = "s")
    @Override
    public PageInfo<ShopQrCode> getPage(ShopQrCode shopQrCode) {
        return new PageInfo<>(getAll(shopQrCode));
    }

    @Override
    public ShopQrCode getById(Long id) {
        return shopQrCodeMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(ShopQrCode shopQrCode) {
        return shopQrCodeMapper.count(shopQrCode);
    }
}
